// mtrand.h
// C++ include file for MT19937, with initialization improved 2002/1/26.
// Coded by Takuji Nishimura and Makoto Matsumoto.
// Ported to C++ by Jasper Bedaux 2003/1/1 (see http://www.bedaux.net/mtrand/).
// The generators returning floating point numbers are based on
// a version by Isaku Wada, 2002/01/09
//
// Copyright (C) 1997 - 2002, Makoto Matsumoto and Takuji Nishimura,
// All rights reserved.
//
// Redistribution and use in source and binary forms, with or without
// modification, are permitted provided that the following conditions
// are met:
//
// 1. Redistributions of source code must retain the above copyright
//    notice, this list of conditions and the following disclaimer.
//
// 2. Redistributions in binary form must reproduce the above copyright
//    notice, this list of conditions and the following disclaimer in the
//    documentation and/or other materials provided with the distribution.
//
// 3. The names of its contributors may not be used to endorse or promote
//    products derived from this software without specific prior written
//    permission.
//
// THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
// "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
// LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR
// A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR
// CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL,
// EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO,
// PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR
// PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF
// LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING
// NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT OF THE USE OF THIS
// SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF SUCH DAMAGE.
//
// Any feedback is very welcome.
// http://www.math.keio.ac.jp/matumoto/emt.html
// email: matumoto@math.keio.ac.jp
//
// Feedback about the C++ port should be sent to Jasper Bedaux,
// see http://www.bedaux.net/mtrand/ for e-mail address and info.

/*
    note from Kill Mario's author:
    I do not use the usual random generator from stdlib.h because with the same seed,
    it doesn't generate the exact same sequence on different architectures running
    different OSes.

    This code (mtrand.h and mtrand.cpp) has been stripped of a lot for KM.
*/

#ifndef MTRAND_H
#define MTRAND_H


#ifdef _MSVC_
#include <SDL/SDL_stdinc.h>
#else
#include <stdint.h>
#endif

class MTRand_int32 { // Mersenne Twister random number generator
    public:
// default constructor: uses default seed only if this is the first instance
        MTRand_int32() { if (!init) seed(5489UL); init = true; }
        void seed(uint32_t); // seed with 32 bit integer
        virtual ~MTRand_int32() {} // destructor
        uint32_t rand_int32(); // generate 32 bit random integer
        float rand_float(float min, float max);
    private:
        static const int32_t n = 624, m = 397; // compile time constants
        static uint32_t state[n]; // state vector array
        static int32_t p; // position in state array
        static bool init; // true if init function is called
        uint32_t twiddle(uint32_t, uint32_t); // used by gen_state()
        void gen_state(); // generate new state
// make copy constructor and assignment operator unavailable, they don't make sense
        MTRand_int32(const MTRand_int32&); // copy constructor not defined
        void operator=(const MTRand_int32&); // assignment operator not defined
};

// inline for speed, must therefore reside in header file
inline uint32_t MTRand_int32::twiddle(uint32_t u, uint32_t v) {
        return (((u & 0x80000000UL) | (v & 0x7FFFFFFFUL)) >> 1)
            ^ ((v & 1UL) ? 0x9908B0DFUL : 0x0UL);
}

inline uint32_t MTRand_int32::rand_int32() { // generate 32 bit random int
        if (p == n) gen_state(); // new state vector needed
// gen_state() is split off to be non-inline, because it is only called once
// in every 624 calls and otherwise irand() would become too big to get inlined
        uint32_t x = state[p++];
        x ^= (x >> 11);
        x ^= (x << 7) & 0x9D2C5680UL;
        x ^= (x << 15) & 0xEFC60000UL;
        return x ^ (x >> 18);
}

inline float MTRand_int32::rand_float(float min, float max) {
    float d = max-min;
    float r = rand_int32()%((int)(100*d));
    r/=100;
    r+=min;
    return r;
}

#endif
